package com.example.demo.configer;

import com.example.demo.manager.publisher.AsynCanalChangePublisherImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author <a href="hedong.wu@baozun.com">wuhedong</a>
 * @Date 2020/5/26 17:30
 **/
@Slf4j
@Configuration
public class ExecutorConfigure {


    @Bean(name = "canalExecutor")
    @ConditionalOnBean(AsynCanalChangePublisherImpl.class)
    public ThreadPoolExecutor threadPoolExecutor() {
        ThreadFactory threadFactory = r -> new Thread(() -> {
            log.info("threadFactory create new thread");
            r.run();
        });
        return new ThreadPoolExecutor(5,
                10,
                50,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1000), threadFactory);
    }

}
